package com.lanling.eduservice.controller;


import com.lanling.eduservice.entity.subject.OneSubject;
import com.lanling.eduservice.orther.R;
import com.lanling.eduservice.service.EduSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author 胡振华
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/eduservice/edu-subject")
@CrossOrigin
public class EduSubjectController {

    @Autowired
    private EduSubjectService eduSubjectService;

    //根据excel文件添加一级二级分类
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file){
        eduSubjectService.saveSubject(file,eduSubjectService);
        return R.ok();
    }

    //拿到所有一级二级课程分类
    @GetMapping("getAllSubject")
    public R getAllSubjects(){
        List<OneSubject> oneSubjects = eduSubjectService.getOneTwoSubjects();
        return R.ok().data("list",oneSubjects);
    }

    //添加一级分类
    @GetMapping("addOneSubject/{title}")
    public R addOneSubject(@PathVariable String title){
        boolean flag = eduSubjectService.addOneSubject(title);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //添加二级分类
    @GetMapping("addTwoSubject/{subjectParentId}/{title}")
    public R addTwoSubject(@PathVariable String subjectParentId,@PathVariable String title){
        boolean flag = eduSubjectService.addTwoSubject(subjectParentId,title);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}